﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLTouch.Client.Metro.Cache;
using SQLTouch.Service.DataContracts;
using SQLTouch.Service.ServiceContracts;

namespace SQLTouch.Client.Metro
{
    public class Facade : IFacade
    {
        private readonly INavigator navigator;
        private readonly ICache<DatabaseData> databaseCache;
        private readonly ICache<QueryData> queryCache;
        private readonly ITouchService touchProxy;

        public event Action<DatabaseData> DatabaseAdded
        {
            add { databaseCache.ItemAdded += value; }
            remove { databaseCache.ItemAdded -= value; }
        }

        public event Action<DatabaseData> DatabaseUpdated
        {
            add { databaseCache.ItemUpdated += value; }
            remove { databaseCache.ItemUpdated -= value; }
        }

        public event Action<DatabaseData> DatabaseDeleted
        {
            add { databaseCache.ItemDeleted += value; }
            remove { databaseCache.ItemDeleted -= value; }
        }

        public event Action<QueryData> QueryAdded
        {
            add { queryCache.ItemAdded += value; }
            remove { queryCache.ItemAdded -= value; }
        }

        public event Action<QueryData> QueryUpdated
        {
            add { queryCache.ItemUpdated += value; }
            remove { queryCache.ItemUpdated -= value; }
        }

        public event Action<QueryData> QueryDeleted
        {
            add { queryCache.ItemDeleted += value; }
            remove { queryCache.ItemDeleted -= value; }
        }

        public Facade(INavigator navigator, ICache<DatabaseData> databaseCache, ICache<QueryData> queryCache, ITouchService touchProxy)
        {
            this.navigator = navigator;
            this.databaseCache = databaseCache;
            this.queryCache = queryCache;
            this.touchProxy = touchProxy;
        }

        public IEnumerable<DatabaseData> GetDatabases()
        {
            return databaseCache.GetItems();
        }

        public void AddDatabase(DatabaseData database)
        {
            databaseCache.Add(database);
        }

        public void UpdateDatabase(DatabaseData database)
        {
            databaseCache.Update(database);
        }

        public void DeleteDatabase(DatabaseData database)
        {
            databaseCache.Delete(database);
        }

        public IEnumerable<QueryData> GetQueries()
        {
            return queryCache.GetItems();
        }

        public void AddQuery(QueryData query)
        {
            queryCache.Add(query);
        }

        public void UpdateQuery(QueryData query)
        {
            queryCache.Update(query);
        }

        public void DeleteQuery(QueryData query)
        {
            queryCache.Delete(query);
        }

        public void ShowAddDatabase()
        {
            navigator.ShowAddDatabase();
        }

        public void ShowEditDatabase(DatabaseData database)
        {
            navigator.ShowEditDatabase(database);
        }

        public void ShowDeleteDatabase(DatabaseData database)
        {
            navigator.ShowDeleteDatabase(database);
        }

        public void ShowAddQuery(DatabaseData database)
        {
            navigator.ShowAddQuery(database);
        }

        public void ShowEditQuery(QueryData query)
        {
            navigator.ShowEditQuery(query);
        }

        public void ShowDeleteQuery(QueryData query)
        {
            navigator.ShowDeleteQuery(query);
        }

        public SchemaData GetSchema(string connectionString)
        {
            return touchProxy.GetSchema(connectionString);
        }

        public QueryResultData GetQueryResult(string connectionString, QueryData query)
        {
            return touchProxy.GetQueryResult(connectionString, query);
        }

        public void ShowUnexpectedError(string errorMessage)
        {
            navigator.ShowUnexpectedError(errorMessage);
        }

        public void ShowUnexpectedError(string errorMessage, Exception exception)
        {
            navigator.ShowUnexpectedError(errorMessage, exception);
        }

        public void GoToHome()
        {
            navigator.GoToHome();
        }

        public void GoToExplore(DatabaseData database)
        {
            navigator.GoToExplore(database);
        }
    }
}
